import { BadRequestException, Controller, Get, Param, Query } from '@nestjs/common';
import { ChatroomService } from './chatroom.service';
import { RequireLogin, UserInfo } from 'src/custom.decorator';


//聊天室接口
@Controller('chatroom')
@RequireLogin()
export class ChatroomController {
  constructor(private readonly chatroomService: ChatroomService) { }

  //创建私聊,需要知道对方id
  @Get('create-one-to-one')
  async oneToOne(@Query('friendId') friednId: number, @UserInfo('userId') userId: number) {
    if (!friednId) {
      throw new BadRequestException('聊天好友的id不能为空')
    }
    return this.chatroomService.createOneToOneChatroom(friednId, userId)
  }

  //创建群聊,需要传入名字
  @Get('create-group')
  async group(@Query('name') name: string, @UserInfo('userId') userId: number) {
    return this.chatroomService.createGroupChatroom(name, userId)
  }


  //查看所有群聊
  @Get('list')
  async list(@UserInfo('userId') userId: number , @Query('name') name:string) {
    if (!userId) {
      throw new BadRequestException('userId 不能为空')
    }
    return this.chatroomService.list(userId,name);
  }

  //获取群内所有用户信息
  @Get('members')
  async members(@Query('chatroomId') chatroomId: number) {
    if (!chatroomId) {
      throw new BadRequestException('chatroomId 不能为空')
    }
    return this.chatroomService.members(chatroomId)
  }

  //获取群聊信息与用户信息
  @Get('info/:id')
  async info(@Param('id') id: number) {
    if (!id) {
      throw new BadRequestException('id 不能为空')
    }
    return this.chatroomService.info(id);
  }

  //加入群聊
  @Get('join/:id')
  async join(@Param('id') id: number, @Query('joinUsername') joinUsername:string) {
    if (!id) {
      throw new BadRequestException('id 不能为空')
    }
    if (!joinUsername) {
      throw new BadRequestException('joinUserId 不能为空')
    }
    return this.chatroomService.join(id, joinUsername);
  }


  //退出群聊
  @Get('quit/:id')
async quit(@Param('id') id: number, @Query('quitUserId') quitUserId: number) {
    if(!id) {
      throw new BadRequestException('id 不能为空')
    }
    if(!quitUserId) {
      throw new BadRequestException('quitUserId 不能为空')
    }
    return this.chatroomService.quit(id, quitUserId);
}

@Get('findChatroom')
async findChatroom(@Query('userId1') userId1: string, @Query('userId2') userId2: string) {
    if(!userId1 || !userId2) {
      throw new BadRequestException('用户 id 不能为空');
    }
    return this.chatroomService.queryOneToOneChatroom(+userId1, +userId2);
}
}


/**
 * 8.11
 * 实现群聊的创建，加入，退出
 * 群聊包括群聊，私聊(一对一群聊，创建聊天室后加入两个用户)
 * 可以使用join单独加人
 * 可以查询聊天室列表
 */